package com.dz.risk.interceptor;


import com.dz.risk.common.exception.RRException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 权限校验拦截器
 *
 * @author yanpanyi
 * @date 2019/4/5
 */
@Component
@Slf4j
public class PermissionInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws RRException, ServletException, IOException {
        String token = "iH3fS8xL0xM7fG1mX1hE4uE2wM7mH4pV3gB1hT5kN9tN3tY4mF6pF4rK4yT6iG6l";
        // String token = request.getHeader(PermissionInterceptor.TOKEN);
        String authorization = request.getHeader("Authorization");
        log.info("==>authorization:" + authorization);
        log.info("==>token:" + token);
        String host = request.getHeader("Host");
        Object user = request.getSession().getAttribute("user");
        Cookie[] cookies = request.getCookies();
        request.getSession().getAttribute("USER");
        return true;
//        if (authorization == null || !authorization.equals(token)) {
//            //未登陆，返回登陆页面
//            request.setAttribute("msg", "没有权限请先登陆");
//            request.setCharacterEncoding("501");
//            response.setStatus(-1);
//            response.sendError(HttpServletResponse.SC_NOT_FOUND, "没有权限请先登陆 -2");
//            log.error("msg:没有权限请先登陆");
//            //  request.getRequestDispatcher("/index.html").forward(request,response);
//            return false;
//        } else {
//            //已登陆，放行请求
//        return true;
//        }

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle .....");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion .....");
    }
}
